//
//  STDSSkinWitenEffect.h
//  STDSRenderSDKIOS
//
//  Created by 陈学明 on 2023/4/3.
//

#ifndef STDSSkinWitenEffect_h
#define STDSSkinWitenEffect_h
#include "STDSBaseEffect.hpp"

class STDSSkinWitenEffect: public STDSBaseEffect {
public:
    STDSSkinWitenEffect() {
        mEffectType = STDSEffectTypeSkinWiten;
    }
    ~STDSSkinWitenEffect() {
//        printf("dealloc--STDSMosaicEffect\n");
    }
    
    void initGL() override {
        STDSBaseEffect::initGL();
    }
    
    void setFloatArguments(const char *name, float *floatValue, int n) override {
        if (strcmp(name, "strength")) {
            mStrength = *floatValue;
        }
    }
    
    void setStringArtuments(const char *name, const char *stringValue) override {
        if (strcmp("portraitTexture", name) == 0) {
            mPortraitPath = string(stringValue);
            mPortraitTex = nullptr;
        } else if (strcmp("skinLutTexture", name) == 0) {
            mLutPath = string(stringValue);
            mLutTex = nullptr;
        }
    }
public:
    shared_ptr<STDSShaderProgram> getEffectProgram() {
        if (mDetectProgram == nullptr) {
            mDetectProgram = mContext->getProgram(kShaderSRCSkinWiten);
        }
        return mDetectProgram;
    }
    
    shared_ptr<STDSTexture> getPortraitTex() {
        if (mPortraitTex == nullptr) {
            mPortraitTex = mContext->getImageTexture(mPortraitPath);
        }
        return mPortraitTex;
    }
    
    shared_ptr<STDSTexture> getLutTex() {
        if (mLutTex == nullptr) {
            mLutTex = mContext->getImageTexture(mLutPath);
        }
        return mLutTex;
    }
    
    void draw() override {
        auto program = getEffectProgram();
        auto portraitTexture = getPortraitTex();
        
        if (program == nullptr || portraitTexture == nullptr || mTexture == nullptr) {
            return;
        }
        
        program->use();
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, mTexture->getId());
        program->setUniform(program->getUniform("inputImageTexture1"), 0);
        
        program->use();
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, portraitTexture->getId());
        program->setUniform(program->getUniform("portraitTexture"), 0);
        
        program->setUniform(program->getUniform("strength"), mStrength);
        mAtbRect.setPosVex(program->getAttrib("aPos"));
        mAtbRect.setPosTex(program->getAttrib("aTexCoord"));
        mAtbRect.draw();
        program->unuse();
    }

private:
    string mPortraitPath{""};
    string mLutPath{""};
    float mStrength{0.0};
    shared_ptr<STDSTexture> mPortraitTex{nullptr};
    shared_ptr<STDSTexture> mLutTex{nullptr};
    shared_ptr<STDSShaderProgram> mDetectProgram{nullptr};
};

#endif /* STDSSkinWitenEffect_h */
